package com.kkb.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.pojo.Doctor;
import com.kkb.service.DoctorService;
import com.kkb.vo.QueryVO;
import com.kkb.vo.ResultVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

@Controller
@RequestMapping("doctor")
@ResponseBody
public class DoctorController {
    @Resource
    private DoctorService doctorService;

    @RequestMapping(value = "list",method = RequestMethod.GET)
    public ResultVO<Doctor> queryByPage(Integer pageNum, Integer pageSize, QueryVO vo){
        if (pageNum == null || pageNum < 0){
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 0){
            pageSize = 5;
        }
        PageInfo<Doctor> doctorPageInfo = doctorService.queryByPage(pageNum, pageSize, vo);
        return new ResultVO<>(doctorPageInfo);
    }
    //添加医生信息
    @RequestMapping(value = "addDoctor",method = RequestMethod.POST)
    public ResultVO<Integer> addDoctor(Doctor doctor){
        Integer add = doctorService.addDoctor(doctor);
        if (add>0){
            return new ResultVO<>();
        }
        return new ResultVO<>(500,"添加失败");
    }
    //修改医生信息
    @RequestMapping(value = "update",method = RequestMethod.PUT)
    public ResultVO<Integer> updateDoctor(Doctor doctor){
        Integer update = doctorService.update(doctor);
        if (update>0){
            return new ResultVO<>(200,"修改成功");
        }else {
            return new ResultVO<>(500,"修改失败");
        }
    }
    //查询单个医生信息
    @RequestMapping(value = "queryById/{d_id}",method = RequestMethod.GET)
    public ResultVO<Doctor> queryByDid(@PathVariable("d_id") Integer d_id){
        Doctor doctor = doctorService.queryById(d_id);
        if (doctor != null){
            return new ResultVO<>(doctor);
        }
        return new ResultVO<>(404,"数据丢失了");
    }
}
